<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>用户信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="../../static/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../../static/user/css/font.css">
    <link rel="stylesheet" href="../../static/user/css/font.css">
    <link rel="stylesheet" href="../../static/user/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="../../static/user/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../static/user/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="username" class="layui-form-label">
                <span class="x-red">*</span>用户名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="username" name="username"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="password" class="layui-form-label">
                <span class="x-red">*</span>密码
            </label>
            <div class="layui-input-inline">
                <input type="text" id="password" name="password"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="nickname" class="layui-form-label">
                <span class="x-red">*</span>昵称
            </label>
            <div class="layui-input-inline">
                <input type="text" id="nickname" name="nickname"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="sex" class="layui-form-label">
                <span class="x-red">*</span>性别
            </label>
            <div class="layui-input-inline">
                <input type="text" id="sex" name="sex"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="tel" class="layui-form-label">
                <span class="x-red">*</span>电话
            </label>
            <div class="layui-input-inline">
                <input type="text" id="tel" name="tel"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="idCard" class="layui-form-label">
                <span class="x-red">*</span>身份证号
            </label>
            <div class="layui-input-inline">
                <input type="text" id="idCard" name="idCard"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="userState" class="layui-form-label">
                <span class="x-red">*</span>用户状态
            </label>
            <div class="layui-input-inline">
                <input type="text" id="userState" name="userState"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <button  class="layui-btn" id="L_repass" type="button" onclick="updateUser()">
                修改
            </button>
        </div>
    </form>
</div>
<script>
    $(document).ready(function () {
        // 数据回显
        let t = '[[${username}]]';
        console.log(t);
        init(t);
    })

    function init(username) {
        $.ajax({
            url: "/user/queryByUserName",
            dataType: "json",
            type: "post",
            data: {
                username: '[[${username}]]',
            },
            success: function (data) {
                let user = data.user;
                console.log("success");
                console.log(user);
                $('#username').val(user.username);
                $('#nickname').val(user.nickname);
                $('#name').val(user.name);
                $('#sex').val(user.sex);
                $('#tel').val(user.tel);
                $('#idCard').val(user.idCard);
                $('#userState').val(user.userState);
                localStorage.setItem("uid", user.uid);
            } , error: function () {

            }
        })

    }
    // 修改用户
    function updateUser() {
        // 做修改
        if(confirm("你确定要修改")) {
            $.ajax({
                url: 'update',
                dataType: "json",
                type: 'post',
                data: {
                    uid: localStorage.getItem('uid'),
                    username: $('#username').val(),
                    password: $('#password').val(),
                    nickname: $('#nickname').val(),
                    name: $('#name').val(),
                    sex: $('#sex').val(),
                    tel: $('#tel').val(),
                    idCard: $('#idCard').val(),
                    userState: $('#userState').val()
                }, success: function (data) {
                    let code = data.code;
                    if(code === 200) {
                        alert("更新成功");
                        //刷新父页面
                        parent.location.reload();
                        // 获得frame索引
                        let index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                    } else {
                        alert("更新失败");
                    }
                }, error: function () {
                    alert("发送失败");
                }
            })
        }
    }
</script>

</body>

</html>